/*
 (C) 2005 Adam D. Bradley
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of Version 2 of the GNU General Public 
 License.
 
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.
 
 You should have received a copy of the GNU Library General Public
 License along with this library; if not, write to the Free
 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package step;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

// SJJ - SMOD: 
//      Changed to use Maps in order to achieve O(1) access time to 
//      parameters within scheduler
//		Also added much needed observer methods
public class FlowType
{
	public static String SCHEDULER = "scheduler";
	public static String SCHEDULERATTRIBUTES = "scheduler_attributes";
	public static String PARAM_SCHEDTYPE = "type";
	public static String PARAM_SCHEDPRIORITY = "priority";
	public static String PARAM_SCHEDCONCURRENCY = "concurrency";
	
	public static String EXPIRY = "expiration";
	
	public FlowType(String Label) {
		label = Label;
		params = new HashMap<String, FlowTypeParam>();
	}
	
	public void AddChild(FlowTypeParam n)
	{
		//		FlowTypeParam newparams[] = new FlowTypeParam[params.length + 1];
		//		System.arraycopy(params, 0, newparams, 0, params.length);
		//		newparams[params.length] = n;
		//		params = newparams;
		params.put(n.getParam(), n);
	}
	
	public void Write(OutputStream out, int depth) throws java.io.IOException
	{
		step.Node.Indent(out, depth);
		out.write(("<flowtype label=\"" + label + "\"").getBytes());
		if (params.keySet().size() > 0) {
			out.write(">\n".getBytes());
			for (FlowTypeParam i: params.values()) {
				i.Write(out, depth+1);
			}
			step.Node.Indent(out, depth);
			out.write(("</flowtype>\n").getBytes());
		} else 
			out.write("/>\n".getBytes());
	}


	public String getLabel() {
		return label;
	}
	
	/**
	 * 
	 * @param name - the label of the flowtype param to return
	 * @return - the FlowTypeParam object
	 * @throws - NoSuchElementException if the label can't be found
	 */
	public FlowTypeParam getParam(String name) 
		throws NoSuchElementException{
		if (params.containsKey(name)) return params.get(name);
		throw new NoSuchElementException("Parameter "+name+" not present");
	}
	
	
	private String label;
	//private FlowTypeParam[] params;
	private Map<String, FlowTypeParam> params;
	
}